import 'dart:io';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getwidget/components/image/gf_image_overlay.dart';
import 'package:getwidget/getwidget.dart';
import 'package:images_picker/images_picker.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/route/route.dart';
import 'package:meal/ui/draw_under_line.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

class RecommendInfo extends StatefulWidget {
  const RecommendInfo({Key? key}) : super(key: key);

  @override
  State<RecommendInfo> createState() => _RecommendInfoState();
}

class _RecommendInfoState extends State<RecommendInfo> {
  String path = '';

  List<_ProductInfo> data = [];

  double get leftWidth {
    return Sizes.default90;
  }

  double get rowHeight {
    return Sizes.default60;
  }

  @override
  void initState() {
    super.initState();
    data.add(_ProductInfo(name: '河村石磨肠粉（双蛋）', price: 250));
    data.add(_ProductInfo(name: '河村石磨肠粉（双蛋加肉）', price: 350));
    data.add(_ProductInfo(name: '河村石磨肠粉（加蛋加肉）', price: 250));
    data.add(_ProductInfo(name: '河村石磨肠粉（夹肉）', price: 250));
    data.add(_ProductInfo(name: '河村石磨肠粉（斋肠）', price: 250));
    data.add(_ProductInfo(name: '河村石磨肠粉（套餐A）', price: 250));
    data.add(_ProductInfo(name: '河村石磨肠粉（套餐B）', price: 250));
    data.add(_ProductInfo(name: '河村石磨肠粉（套餐C）', price: 250));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ToolsUtils.getDefaultBackgroundColor(),
      appBar: ToolsUtils.getDefaultBrnAppBar('推荐活动详情'),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(children: [
                              TextSpan(
                                text: '名称',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                              ),
                            ]))),
                        Expanded(
                            child: Container(
                          height: Sizes.default45,
                          padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          margin: const EdgeInsets.only(right: Sizes.default15),
                          decoration: BoxDecoration(
                              color: ColorsUtils.rgba(247, 247, 247, 1),
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(Sizes.default10))),
                          child: TextField(
                            controller: ToolsUtils.editingController('暑假优惠大酬宾'),
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color363, fontSize: Sizes.default15),
                            decoration: InputDecoration(
                                border: const OutlineInputBorder(
                                  borderSide: BorderSide.none,
                                ),
                                isCollapsed: true,
                                filled: true,
                                fillColor: Colors.transparent,
                                contentPadding: const EdgeInsets.symmetric(
                                    horizontal: Sizes.default10, vertical: Sizes.default10),
                                constraints: const BoxConstraints(minHeight: Sizes.default45),
                                hintText: '例如398',
                                hintStyle: StylesUtils.customTextStyle(
                                    fontSize: Sizes.default14, color: ColorsUtils.color999)),
                            keyboardType: TextInputType.text,
                            onChanged: (String content) {},
                          ),
                        )),
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(
                                text: '活动日期',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333)))),
                        Expanded(
                          child: InkWell(
                            child: Container(
                              height: Sizes.default45,
                              padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                              alignment: Alignment.centerLeft,
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text('2023-01-01 至 2023-11-31',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: Sizes.default15, color: ColorsUtils.color797)),
                                  BrunoTools.getAssetSizeImage(BrnAsset.iconRightArrow, 16, 16)
                                ],
                              ),
                            ),
                            onTap: () {
                              ToolsUtils.openDateRangePicker(context, onConfirm:
                                  (DateTime startDateTime, DateTime endDateTime,
                                      List<int> startSelectedIndex, List<int> endSelectedIndex) {
                                setState(() {});
                              });
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.centerLeft,
                            child: Text.rich(TextSpan(
                                text: '活动状态',
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color333)))),
                        Expanded(
                            child: Container(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            '进行中',
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color333),
                          ),
                        ))
                      ],
                    ),
                  ),
                  Container(
                    height: rowHeight,
                    alignment: Alignment.centerLeft,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                            width: leftWidth,
                            alignment: Alignment.topLeft,
                            child: Text.rich(
                              TextSpan(
                                  text: '备注说明',
                                  style: StylesUtils.customTextStyle(
                                      fontSize: Sizes.default15, color: ColorsUtils.color333)),
                            )),
                        Expanded(
                            child: Container(
                          padding: const EdgeInsets.symmetric(horizontal: Sizes.default10),
                          decoration: BoxDecoration(
                              color: ColorsUtils.rgba(247, 247, 247, 1),
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(Sizes.default10))),
                          child: BrnInputText(
                            hint: '0/500',
                            maxLength: 500,
                            autoFocus: false,
                            textString: '每人限购一份',
                          ),
                        ))
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding:
                  const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default8),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  Container(
                      margin: const EdgeInsets.only(bottom: Sizes.default15),
                      child: Text.rich(
                        TextSpan(
                            text: '中文海报',
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color333)),
                      )),
                  Stack(
                    children: [
                      GFImageOverlay(
                        height: 92,
                        borderRadius: BorderRadius.circular(Sizes.default5),
                        image: img,
                      ),
                      Positioned(
                          width: Sizes.default20,
                          height: Sizes.default20,
                          right: Sizes.default10,
                          bottom: Sizes.default10,
                          child: IconButton(
                              onPressed: () {
                                ToolsUtils.openGalleryOrCamera(context, (res) {}, (res) {}, 1);
                              },
                              icon: ToolsUtils.getUrlSvg(
                                  'https://cdn7.axureshop.com/demo/1525603/images/推荐活动详情/u5153.svg',
                                  width: Sizes.default20,
                                  height: Sizes.default15)))
                    ],
                  )
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: Sizes.default20),
              padding: const EdgeInsets.symmetric(horizontal: Sizes.default15),
              decoration: const BoxDecoration(color: Colors.white),
              child: ListBody(
                children: [
                  DrawUnderLine(
                      color: ColorsUtils.parseColorFromHexString('D7D7D7'),
                      child: SizedBox(
                        height: Sizes.default50,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '推荐商品',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color333, fontSize: Sizes.default15),
                            ),
                            TextButton(
                                onPressed: () {
                                  Get.toNamed(RouteGet.marketRecommendAdd);
                                },
                                child: Text.rich(TextSpan(
                                    text: '添加商品(9/20)',
                                    style: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default15, color: ColorsUtils.color797),
                                    children: [
                                      WidgetSpan(
                                          child: ToolsUtils.iconRightArrow(),
                                          alignment: PlaceholderAlignment.middle)
                                    ])))
                          ],
                        ),
                      )),
                  ...data
                      .map((e) => Container(
                            margin: const EdgeInsets.only(top: Sizes.default10),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  '·  ${e.name}',
                                  style: style,
                                ),
                                TextButton(
                                    onPressed: () {
                                      setState(() {
                                        data.remove(e);
                                      });
                                    },
                                    child: Text.rich(TextSpan(
                                        text: '￥${e.price}',
                                        style: defaultStyle,
                                        children: [
                                          TextSpan(
                                              text: '  x',
                                              style: StylesUtils.customTextStyle(
                                                  color: ColorsUtils.colorCcc))
                                        ])))
                              ],
                            ),
                          ))
                      .toList()
                ],
              ),
            )
          ],
        ),
      ),
      bottomNavigationBar: SafeArea(
        top: false,
        child: Container(
          height: Sizes.default60,
          padding: const EdgeInsets.only(right: Sizes.default20, left: Sizes.default40),
          child: Row(
            children: [
              Expanded(
                  child: Container(
                alignment: Alignment.center,
                padding: const EdgeInsets.only(right: Sizes.default30),
                child: FractionallySizedBox(
                  heightFactor: 0.8,
                  widthFactor: 1.0,
                  child: GFButton(
                      onPressed: () {
                        Get.back();
                      },
                      text: '保存',
                      textStyle: StylesUtils.customTextStyle(fontSize: Sizes.default16),
                      shape: GFButtonShape.pills,
                      type: GFButtonType.solid,
                      size: GFSize.LARGE,
                      color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf6)),
                ),
              )),
              Center(
                child: TextButton(
                    onPressed: () {
                      Get.back();
                    },
                    child: Text(
                      '删除',
                      style: StylesUtils.customTextStyle(
                          fontSize: Sizes.default15, color: ColorsUtils.colorFf0),
                    )),
              )
            ],
          ),
        ),
      ),
    );
  }

  void uploadFilePath(List<Media> res) {
    Media media = res[0];
    path = media.path;
    setState(() {});
  }

  dynamic get img {
    return path.isNotEmpty ? FileImage(File(path)) : ToolsUtils.assetImage('favorable.jpg');
  }

  TextStyle get style {
    return StylesUtils.customTextStyle(color: ColorsUtils.color333);
  }

  TextStyle get defaultStyle {
    return StylesUtils.customTextStyle(color: ColorsUtils.color797, fontSize: Sizes.default15);
  }
}

class _ProductInfo {
  String name;
  int price;

  _ProductInfo({required this.name, required this.price});
}
